package com.rocket.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.rocket.R;

public class MenuUtil implements OnKeyListener{

	private Context mContext;

	private View view;

	private PopupWindow popupWindow = null;

	private LinearLayout mMainLayout;

	private LinearLayout topLayout;//

	private LinearLayout bottomLayout;// 

	private Button[] buttons;

	private String[] optionsText;

	private Integer[] optionsImage;

	public MenuUtil() {
	}

	public MenuUtil(Context context, View view, String[] texts, Integer[] images) {
		this.mContext = context;
		this.optionsText = texts;
		this.optionsImage = images;
		this.view = view;
		
		buttons = new Button[8];
		mMainLayout = new LinearLayout(mContext);
		mMainLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		mMainLayout.setOrientation(LinearLayout.VERTICAL);
		mMainLayout.setBackgroundResource(R.drawable.menu_bg);
		mMainLayout.setFocusableInTouchMode(true);
		
		
		mMainLayout.setOnKeyListener(this);
		
		topLayout = new LinearLayout(mContext);
		topLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		topLayout.setGravity(Gravity.CENTER_VERTICAL);

		for (int i = 0; i < 4; i++) {
			Button button = new Button(mContext);
			button.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT, 1));
			if (i < optionsImage.length) {
				button.setTag(i);
				button.setBackgroundResource(R.drawable.menu_select_bg);
				button.setCompoundDrawablesWithIntrinsicBounds(0,
						optionsImage[i], 0, 0);
				button.setText(optionsText[i]);
				button.setTextSize(12);
				button.setTextColor(Color.BLACK);
				button.requestFocus();
			} else {
				button.setVisibility(View.INVISIBLE);
			}
			buttons[i] = button;
			topLayout.addView(button);
		}
		mMainLayout.addView(topLayout);
		bottomLayout = new LinearLayout(mContext);
		bottomLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		bottomLayout.setGravity(Gravity.CENTER_VERTICAL);
		bottomLayout.setVisibility(View.GONE);
		if (optionsText.length > 4) {
			for (int i = 4; i < 8; i++) {
				Button button = new Button(mContext);
				button.setLayoutParams(new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT, 1));
				if (i < optionsImage.length) {
					button.setTag(i);
					button.setBackgroundResource(R.drawable.menu_select_bg);
					button.setCompoundDrawablesWithIntrinsicBounds(0,
							optionsImage[i], 0, 0);
					button.setText(optionsText[i]);
					button.setTextSize(12);
					button.setTextColor(Color.BLACK);
					button.requestFocus();
				} else {
					button.setVisibility(View.INVISIBLE);
				}
				buttons[i] = button;
				bottomLayout.addView(button);
				bottomLayout.setVisibility(View.VISIBLE);
			}
			mMainLayout.addView(bottomLayout);
		}
	}

	public void show() {
		popupWindow = new PopupWindow(mMainLayout, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT, true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
		popupWindow.update();
	}

	public void dismiss() {
		popupWindow.dismiss();
	}

	public void setMenuItemSelectedLinstener(OnClickListener listener) {
		for (int i = 0; i < optionsText.length; i++) {
			buttons[i].setOnClickListener(listener);
		}
	}

	public boolean isShow() {
		if (popupWindow != null && popupWindow.isShowing()) {
			return true;
		} else {
			return false;
		}
	}

	public boolean onKey(View v, int keyCode, KeyEvent event) {
		
		if(keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_DOWN) {
			popupWindow.dismiss();
		}
		return true;
	}
}
